<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<table id="adminData"></table> 

<div id="adminGrid_bar" style="padding:5px;height:auto; display: none">
				<a href="javascript:void(0)" onclick="delAdmin()" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">删除</a>
				<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="appendAdmin()">添加</a>
				<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="updateAdmin()">修改</a>
		</div> 
		
<div id="adminGridwin"  style=" display: none">
				<div style="padding:10px 60px 20px 60px">
	    <form id="adminform" class="easyui-form" method="post" data-options="novalidate:true" action="" >
	    	<table cellpadding="5">
	    		<tr>
	    			<td>用户名:</td>
	    			<td><input id="uName" class="easyui-textbox" type="text" name="uName" data-options="required:true"></input></td>
	    		</tr>
	    		<tr>
	    			<td>密码:</td>
	    			<td><input id="uPwd" class="easyui-textbox" type="password" name="uPwd" data-options="required:true"></input></td>
	    		</tr>
	    		<tr>
	    			<td>重复密码:</td>
	    			<td><input id="ruPwd" class="easyui-textbox" type="password" name="ruPwd" data-options="required:true"></input></td>
	    		</tr>
	    		<tr>
	    			<td>邮箱:</td>
	    			<td><input id="uEmail" class="easyui-textbox" type="text" name="uEmail" data-options="required:true"></input></td>
	    		</tr>
	    		<tr>
	    			<td>手机号码:</td>
	    			<td><input id="uPhone" class="easyui-textbox" type="text" name="uPhone" data-options="required:true"></input></td>
	    		</tr>
	    		<tr>
	    		<td>手机验证码:</td>
	    		<td><input id="code" type="text" class="easyui-numberbox" value="" data-options="min:0,precision:0,required:true"></input>
	    		<a id="phoneButton" href="javascript:void(0)" class="easyui-linkbutton" onclick="getPhoneCode()">获取验证码</a>
	    		<input type="hidden" id="codeData"/>
	    		</td>
	    		</tr>
	    	</table>
	    	<input type="hidden" id="uId" name="uId"/>
	    </form>
	    <div style="text-align:center;padding:5px">
	    	<a id="adminDataButton" href="javascript:void(0)" class="easyui-linkbutton" onclick="submitAdminForm()">确定</a>
	    </div>
	    </div>
	</div>  		
<script type="text/javascript">
		/* 读取用户信息并设置到表格 */
		 var adminData=$('#adminData').datagrid({
			singleSelect:true,
			url:'admin/getAdminList',
			striped:true,
			loadMsg:"正在加载中...",
			pagination:true,
			pageNumber:1,
			pageSize:10,
			pageList:[10,20,30,40,50],
			remoteSort:false,
			fit:true,
			toolbar:"#adminGrid_bar",
			columns:[[
			      	{field:'uId',sortable:true,align:'center',checkbox:true},
			  		{field:'uName',title:'用户名',width:150,align:'center'},
			  		{field:'uEmail',title:'邮箱',width:150,align:'center'},
			  		{field:'uPhone',title:'联系方式',width:150,align:'center'},
			  		{field:'uAddress',title:'上次登陆ip',width:150,align:'center'},
			  		{field:'uStatus',title:'状态',width:180,align:'center'},
			  		{field:'uRegisterTime',title:'添加时间',width:180,align:'center',
			  			formatter: function(value,row,index){
							return getDate(row.uRegisterTime.toString());
			  			}
			  		},
			  		{field:'updatetime',title:'修改时间',width:180,align:'center',
			  			formatter: function(value,row,index){
			  				if(row.updatetime!=""&&row.updatetime!=undefined){
			  					return getDate(row.updatetime.toString());
			  				}
			  			}	
			  		}]]
			
		}); 
		 $("#adminGridwin").removeAttr("style");//移除表单加载前设置的display属性，防止初始加载显示表单
	var editStatus;
	/* 	 添加用户 */
	function appendAdmin(){
		$("#uId").val("");
		$('#adminform').form('clear');
		editStatus="add";
		$('#adminGridwin').dialog({
			title:'添加',
			closed:false
		});
		
	}
	function updateAdmin(){
		$('#adminform').form('clear');
		var rows=adminData.datagrid("getSelections");
		if(rows.length>0){
			$("#uId").val(rows[0].uId);
			editStatus="update";
			$('#adminGridwin').dialog({
				title:'修改--请填写需要修改的项即可',
				closed:false
			});
			$('#uName').textbox({
				value:rows[0].uName
			});
			
		}else{
			$.messager.alert('提示','请选择要修改的行...','info');  
		}
	}
	$('#phoneButton').linkbutton({disabled:false});
	/*获取手机验证码*/
	function getPhoneCode(){
		var phone;
		if(editStatus=="update"){
			phone=adminData.datagrid("getSelections")[0].uPhone;
		}else{
			phone=$.trim($("#uPhone").val());
		}
		getCode(phone);
		
	}
	var InterValObj; //timer变量，控制时间
	var count = 60; //间隔函数，1秒执行
	var curCount;//当前剩余秒数
	 function getCode(phone){
		 curCount=60;
		if(checkPhone(phone)){//合法手机号码
			$('#phoneButton').linkbutton({disabled:true});
			$("#phoneButton").linkbutton({
				text:curCount+"s"
			});
			InterValObj = window.setInterval(SetRemainTime, 1000);
			$.get("common/getPhoneCode",{phone:phone},function(data){
				if(data.status>0){
					$("#codeData").val(data.code);
					$.messager.show({
						title:'成功提示',
						msg:'验证码发送成功',
						timeout:3000,
						showType:'slide'
					});

				}else{
					$.messager.alert('错误','验证码获取异常!','error');
				}
			});
		}else{
			$.messager.alert('错误','您输入的手机号码不合法!','error');
		}
	} 
	 function SetRemainTime(){
		if (curCount == 0) {
			$("#codeData").val("");
	        window.clearInterval(InterValObj);//停止计时器
	        $('#phoneButton').linkbutton({disabled:false});//启用按钮
	        $("#phoneButton").linkbutton({
				text:"重发验证码"
			});
	       
	    }else {
	        curCount--;
	        $("#phoneButton").linkbutton({
				text:curCount+"s"
			});
	    }
	} 
	 function submitAdminForm(){
		 var url;
		 if(editStatus=="add"){
			 url='admin/addAdmin';
		 }else if(editStatus=="update"){
			 url='admin/updateAdmin';
		 }
		 $('#adminform').form('submit',{
				url:url,
				onSubmit:function(){
					if($("#codeData").val()!=$("#code").val()||$("#code").val()==""){
						$.messager.alert('错误','验证码错误...','error');  
						return false;
					}
					 if($("#uName").val().length<2){
						$.messager.alert('错误','用户名长度不能太短...','error');  
						return false;
					}
					 if(editStatus=="add"){
						 if($("#uPwd").val()==""){
								$.messager.alert('错误','密码不能为空...','error');  
								return false;
							}
						 if(!checkPhone($("#uPhone").val())){
								$.messager.alert('错误','手机号码不合法...','error');  
								return false;
							}
							 if($("#uEmail").val()==""){
									$.messager.alert('错误','邮箱输入有误..','error');  
									return false;
								}
					 }else if(editStatus=="update"){
						if($("#uPhone").val()!=""){
							if(!checkPhone($("#uPhone").val())){
								$.messager.alert('错误','手机号码不合法...','error');  
								return false;
							}
						}
					 }
					 if($("#uPwd").val()!=$("#ruPwd").val()){
							$.messager.alert('错误','密码输入不一致...','error');  
							return false;
						}
					
					return true;
				},
				success:function(data){
					if(data==200){//200返回成功的标志
						$('#adminGridwin').dialog('close');
						$.messager.show({
							title:'成功提示',
							msg:'操作成功',
							timeout:5000,
							showType:'slide'
						});
						editStatus=undefined;
						$('#adminData').datagrid("reload");
					}else{
						$.messager.alert('错误','用户名已被使用...');  
					}
				}
				})
		 
	 }
	 
		</script>